﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Text;
using FTchina.Library;
using System.Net;

using System.IO;
using ExtAspNet;
using FTchina.DAL;
namespace FTchina
{
    /// <summary>
    /// basic 的摘要说明
    /// </summary>
    public static class basic
    {
        static CookieContainer cc = new CookieContainer();

        #region 验证访问时间间隔
        /// <summary>
        /// 查看刷新时间
        /// </summary>
        /// <returns></returns>
        public static void CheckFreshTime(Page ValidatePage)
        {
            //int NoPass = 10;//封禁时间间隔10s
            //double DoubleFresh = 0.5;//触发非法刷新时间间隔
            //int WeiFaLimite = 2;//触发封禁时间的上限
            //ValidatePage.Session["NoPass"] = NoPass;
            //double jiange = DateTime.Now.Subtract(Convert.ToDateTime(ValidatePage.Session["LastVisit"])).TotalSeconds;

            //if (ValidatePage.Session["FirstVisit"] == "1")
            //{//检测是否第一次访问
            //    ValidatePage.Session["FirstVisit"] = "0";
            //}
            //else
            //{//不是第一次访问
            //    if (Convert.ToInt32(ValidatePage.Session["BadCount"]) > WeiFaLimite)
            //    {//检测是否有刷的嫌疑
            //        if (jiange < Convert.ToInt32(ValidatePage.Session["NoPass"]))
            //        {//时间间隔小于封禁时间
            //            ValidatePage.Response.Write("您已经被封禁请稍后再试");
            //            ValidatePage.Response.Write(" <META http-equiv='refresh' content='3;url=" + ValidatePage.Request.ServerVariables["HTTP_REFERER"] + "'>");
            //            ValidatePage.Response.End();
            //            return;
            //        }
            //        else
            //        {
            //            ValidatePage.Session["BadCount"] = 0;
            //        }
            //    }
            //    else
            //    {//正常访问
            //        if (jiange < DoubleFresh)
            //        {//时间间隔小于两次刷新最小间隔
            //            ValidatePage.Session["BadCount"] = Convert.ToInt32(ValidatePage.Session["BadCount"]) + 1;
            //            ValidatePage.Response.Write("请不要频繁刷新！违法次数：" + ValidatePage.Session["BadCount"].ToString());
            //            ValidatePage.Response.Write(" <META http-equiv='refresh' content='3;url=" + ValidatePage.Request.ServerVariables["HTTP_REFERER"] + "'>");
            //            ValidatePage.Response.End();
            //            return;
            //        }
            //        else
            //        {
            //            ValidatePage.Session["BadCount"] = 0;
            //            ValidatePage.Session["LastVisit"] = DateTime.Now.ToString();
            //        }

            //    }

            //}
        }
        #endregion

        #region 验证是否有管理权限
        /// <summary>
        /// 获取管理权限
        /// </summary>
        /// <param name="UserPage">验证页面</param>
        /// <param name="Area">管辖区域</param>
        /// <returns></returns>
        public static void CheckAdmin(Page ValidatePage)
        {
            Logic.IPtableLogic.CheckUserIP(ValidatePage);
            if (!MasterHelper.IsLogin)
            {
                FTchina.Logic.AlertLogic.ShowResultAndRedirect("请先登陆系统", "错误！", ExtAspNet.MessageBoxIcon.Error, "/ui/login.aspx", true);

            }
            //   if (ValidatePage.Session["master_id"] == null) ValidatePage.Response.Redirect(GetSiteUrl() + "ui/login.aspx?title=安全检查失败！请重新登陆");
            //string temp = string.Empty;
            //if (FTchina.Caching.FTchinaCache.IsExits(ValidatePage.Session["luruid"].ToString() + "admincookiess"))
            //{
            //    temp = FTchina.Caching.FTchinaCache.GetCache(ValidatePage.Session["luruid"].ToString() + "admincookiess");
            //}
            //else
            //{
            //    temp = DBHelper.GetValueByKey("cookiess", "master", "nicheng='" + ValidatePage.Session["nicheng"].ToString() + "'");
            //    temp = MD5MK.MD5String(temp);
            //    System.Web.HttpContext.Current.Cache.Insert(ValidatePage.Session["luruid"].ToString() + "admincookiess", temp, null, DateTime.Now.AddSeconds(2), TimeSpan.Zero);

            //}
            //if (temp != ValidatePage.Session["master_id"].ToString()) ValidatePage.Response.Redirect(GetSiteUrl() + "ui/login.aspx?title=您已经在其他地方登录！");
        }


        /// <summary>
        /// 检测是否有权限执行相关代码
        /// </summary>
        /// <returns>true，false</returns>
        public static bool ChkPoewr(Page ValidatePage)
        {
            return (MasterHelper.UserGroup == "1");  //系统管理员权限
        }
        /// <summary>
        /// 检测是否有权限执行相关代码
        /// </summary>
        /// <returns>true，false</returns>
        public static void ChkPoewr(Page ValidatePage, string pageurl)
        {

            string power = DBHelper.GetValueByKey("power", "menulist", "link like'%" + pageurl + "%'", "power" + pageurl);
            if (power.Length != 0)
            {
                if (!StringHelper.ChkISinArray(MasterHelper.UserGroup, StringHelper.String2ArrayList(power)))
                {
                    ValidatePage.Response.Write(MasterHelper.MasterName + "无权访问此页");
                    ValidatePage.Response.End();
                }

            }

        }




        #endregion


        #region 字符操作过滤SQL

        /**/
        /// <summary>
        /// 过滤SQL
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string FilterSQL(string text)
        {
            string validSql = "";
            if (text != null)
            {
                text = text.Trim();
                text = text.Replace(";", "'';''");
                text = text.Replace("'", "''");
                text = text.Replace("--", "''--''");
                text = text.Replace("%25", "");
                text = text.Replace("%0a", "");
                text = text.Replace("%22", "");
                text = text.Replace("%27", "");
                text = text.Replace("%5c", "");
                text = text.Replace("%2f", "");
                text = text.Replace("%3c", "");
                text = text.Replace("%3e", "");
                text = text.Replace("%26", "");
                text = text.Replace("<", "&lt;");
                text = text.Replace(">", "&gt;");
                validSql = text;
            }
            return validSql;
        }



        public static string SafeRequest(string str)
        {
            string validSql = "";
            if (str != null)
            {
                str = str.Trim();
                str = str.Replace('\'', ' ');

                str = str.Replace("&nbsp;", "");
                str = str.Replace("\n;", "");
                validSql = str;
            }
            return validSql;
        }

        #endregion





        #region 获取指定时间在全年所处的周数
        /// <summary> 
        /// 取指定日期是一年中的第几周 
        /// </summary> 
        /// <param name="dtime">给定的日期</param> 
        /// <returns>数字 一年中的第几周</returns> 
        public static int weekofyear(DateTime dtime)
        {
            int weeknum = 0;
            DateTime tmpdate = DateTime.Parse(dtime.Year.ToString() + "-1" + "-1");
            DayOfWeek firstweek = tmpdate.DayOfWeek;
            //if(firstweek) 
            for (int i = (int)firstweek + 1; i <= dtime.DayOfYear; i = i + 7)
            {
                weeknum = weeknum + 1;
            }
            return weeknum;
        }
        #endregion

        #region 新闻系统常用的字符 html等操作


        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="Str">所要截取的字符串</param>
        /// <param name="Num">截取字符串的长度</param>
        /// <returns></returns>
        public static string GetSubString(string Str, int Num)
        {
            if (Str == null || Str == "")
                return "";
            string outstr = "";
            int n = 0;
            foreach (char ch in Str)
            {
                n += System.Text.Encoding.Default.GetByteCount(ch.ToString());
                if (n > Num)
                    break;
                else
                    outstr += ch;
            }
            return outstr;
        }
        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="Str">所要截取的字符串</param>
        /// <param name="Num">截取字符串的长度</param>
        /// <param name="Num">截取字符串后省略部分的字符串</param>
        /// <returns></returns>
        public static string GetSubString(string Str, int Num, string LastStr)
        {
            return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str;
        }


        /// <summary>
        /// 验证字符串是否是图片路径
        /// </summary>
        /// <param name="Input">待检测的字符串</param>
        /// <returns>返回true 或 false</returns>
        public static bool IsImgString(string Input)
        {
            return IsImgString(Input, "/{@dirfile}/");
        }

        public static bool IsImgString(string Input, string checkStr)
        {
            bool re_Val = false;
            if (Input != string.Empty)
            {
                string s_input = Input.ToLower();
                if (s_input.IndexOf(checkStr.ToLower()) != -1 && s_input.IndexOf(".") != -1)
                {
                    string Ex_Name = s_input.Substring(s_input.LastIndexOf(".") + 1).ToString().ToLower();
                    if (Ex_Name == "jpg" || Ex_Name == "gif" || Ex_Name == "bmp" || Ex_Name == "png")
                    {
                        re_Val = true;
                    }
                }
            }
            return re_Val;
        }


        /// <summary>
        ///  将字符转化为HTML编码
        /// </summary>
        /// <param name="str">待处理的字符串</param>
        /// <returns></returns>
        public static string HtmlEncode(string Input)
        {
            return HttpContext.Current.Server.HtmlEncode(Input);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string HtmlDecode(string Input)
        {
            return HttpContext.Current.Server.HtmlDecode(Input);
        }

        /// <summary>
        /// URL地址编码
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string URLEncode(string Input)
        {
            return HttpContext.Current.Server.UrlEncode(Input);
        }

        /// <summary>
        /// URL地址解码
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string URLDecode(string Input)
        {
            return HttpContext.Current.Server.UrlDecode(Input);
        }

        /// <summary>
        /// 敏感字符过滤
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string Filter(string sInput)
        {
            if (sInput == null || sInput == "")
                return null;
            string sInput1 = sInput.ToLower();
            string output = sInput;
            string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
            if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
            {
                throw new Exception("字符串中含有非法字符!");
            }
            else
            {
                output = output.Replace("'", "''");
            }
            return output;
        }

        /// <summary>
        /// 过滤特殊字符/前台会员
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string Htmls(string Input)
        {
            if (Input != string.Empty && Input != null)
            {
                string ihtml = Input.ToLower();
                ihtml = ihtml.Replace("<script", "&lt;script");
                ihtml = ihtml.Replace("script>", "script&gt;");
                ihtml = ihtml.Replace("<%", "&lt;%");
                ihtml = ihtml.Replace("%>", "%&gt;");
                ihtml = ihtml.Replace("<$", "&lt;$");
                ihtml = ihtml.Replace("$>", "$&gt;");
                return ihtml;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 把HTML代码转换成TXT格式
        /// </summary>
        /// <param name="chr">等待处理的字符串</param>
        /// <returns>处理后的字符串</returns>


        public static string ForTXT(string Input)
        {
            StringBuilder sb = new StringBuilder(Input);
            sb.Replace("<font", " ");
            sb.Replace("<span", " ");
            sb.Replace("<style", " ");
            sb.Replace("<div", " ");
            sb.Replace("<p", "");
            sb.Replace("</p>", "");
            sb.Replace("<label", " ");
            sb.Replace("&nbsp;", " ");
            sb.Replace("<br>", "");
            sb.Replace("<br />", "");
            sb.Replace("<br />", "");
            sb.Replace("&lt;", "");
            sb.Replace("&gt;", "");
            sb.Replace("&amp;", "");
            sb.Replace("<", "");
            sb.Replace(">", "");
            return sb.ToString();
        }
        /// <summary>
        /// 把TXT代码转换成HTML格式
        ///  </summary>
        /// <param name="chr">等待处理的字符串</param>
        /// <returns>处理后的字符串</returns>


        public static String ToHtml(string Input)
        {
            StringBuilder sb = new StringBuilder(Input);
            sb.Replace("&", "&amp;");
            sb.Replace("<", "&lt;");
            sb.Replace(">", "&gt;");
            sb.Replace("\r\n", "<br />");
            sb.Replace("\n", "<br />");
            sb.Replace("\t", " ");
            sb.Replace(" ", "&nbsp;");
            return sb.ToString();
        }




        /// <summary>
        /// 去除html标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealHtml(string str)
        {
            str = Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(script)[^>]*>|<\/(script)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<br[^>]*", "", RegexOptions.IgnoreCase);
            return str.Replace("&nbsp;", "");
        }


        /// <summary>
        /// 去除script和A超链接
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealScript(string str)
        {
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(script)[^>]*>|<\/(script)>", "", RegexOptions.IgnoreCase);
            return str;
        }

        /// <summary>
        /// 去除<a></a>
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealA(string str)
        {
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            return str.Replace("&nbsp;", "");
        }
        public static string DealINPUT(string str)
        {
            str = Regex.Replace(str, @"<input[^>]*/>", "", RegexOptions.IgnoreCase);
            return str.Replace("&nbsp;", "");
        }



        #endregion


        #region 将aspx页面作为js输出提供者
        public static string JsOutput(string words)
        {
            return "document.write('" + words.Replace("'", "") + "')";
        }
        #endregion




        #region 操作  随机数  数据
        /// <summary>
        /// 获取随机数类型枚举对应的数字
        /// </summary>
        /// <param name="Enum">产生随机数的枚举类型</param>
        /// <returns>int</returns>
        private static int RandInt(RandEnum Enum) { return (int)Enum; }
        /// <summary>
        /// 设置随机数类型的枚举
        /// </summary>
        public enum RandEnum : int
        {
            /// <summary>
            /// 数字
            /// </summary>
            Numeric = 0,
            /// <summary>
            /// 字母
            /// </summary>
            Letter = 1,
            /// <summary>
            /// 数字字母混合
            /// </summary>
            Blend = 2,
            /// <summary>
            /// 汉字
            /// </summary>
            Chinese = 3
        }
        /// <summary>
        /// 获取全局唯一GUID 值
        /// </summary>
        /// <returns>string</returns>
        public static string GetGuid() { return GetGuid(100, true); }
        /// <summary>
        /// 获取当前时间的刻度值
        /// </summary>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>int</returns>
        public static int GetGuid(int MaxLength) { return Convert.ToInt32((Right(DateTime.Now.Ticks.ToString(), 9))); }
        public static string GetStringGuid(int MaxLength) { return (Right(DateTime.Now.Ticks.ToString(), MaxLength)); }
        /// <summary>
        /// 获取全局唯一GUID 值
        /// </summary>
        /// <param name="MaxLength">最大长度</param>
        /// <param name="IsRemove">是否去掉特殊字符</param>
        /// <returns>string</returns>
        public static string GetGuid(int MaxLength, bool IsRemove)
        {
            string Guids = Guid.NewGuid().ToString();
            if (IsRemove) { Guids = Guids.Replace("{", ""); Guids = Guids.Replace("}", ""); Guids = Guids.Replace("-", ""); }
            Guids = Left(Guids, MaxLength);
            return Guids;
        }
        /// <summary>
        /// 获取指定类型的随机数
        /// </summary>
        /// <param name="Enum">产生随机数的枚举类型</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string GetGuid(RandEnum Enum, int MaxLength)
        {
            return GetGuid(Enum, "GB2312", MaxLength);
        }
        /// <summary>
        /// 获取指定类型的随机数
        /// </summary>
        /// <param name="Enum">产生随机数的枚举类型</param>
        /// <param name="Encode">产生随机中文的编码</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string GetGuid(RandEnum Enum, string Encode, int MaxLength)
        {
            if (Enum == RandEnum.Chinese) return GetGuid(Encode, MaxLength);
            string Guids = "";
            for (int i = 1; i <= MaxLength; i++)
            {
                int MinInt, MaxInt, Num;
                Random Rand = new Random(GetGuid(9) + i * 1000);
                Num = (Enum == RandEnum.Blend) ? ((Rand.Next(0, 10) <= 4) ? (int)RandEnum.Numeric : (int)RandEnum.Letter) : (int)Enum;
                MinInt = Num == 0 ? 48 : 97;
                MaxInt = Num == 0 ? 57 : 122;
                Guids += Rand.Next(MinInt, MaxInt).ToString();
                Rand = null;
            }
            return Guids;
        }
        /// <summary>
        /// 获取随机产生的中文
        /// </summary>
        /// <param name="Encode">产生随机中文的编码</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string GetGuid(string Encode, int MaxLength)
        {
            //定义返回的字符串
            string Chinese = "";
            //定义中文编码
            Encoding Ecode = Encoding.GetEncoding(Encode);
            Random Rnd = null;// new Random(GetIntRand(4));
            //int Rint = Rnd.Next(1, 100);
            //定义位码、区码的范围数
            int Wint, Qint;
            for (int i = 1; i <= MaxLength; i++)
            {
                int Rint = 0;
                //获取汉字区位
                Rnd = new Random(GetGuid(9) + i * 1000);
                Rint = Rnd.Next(16, 56);//只获取常用汉字 16-55之间
                Wint = Rint;
                //55区只89个汉字 其他 94个汉字
                Rint = (Wint == 55) ? 90 : 95;
                //定义新种子
                Rnd = new Random(GetGuid(9) + i * 3000);
                Rint = Rnd.Next(1, Rint);
                Qint = Rint;
                //两个字节变量存储产生的随机汉字区位码
                byte Fbyte = System.Convert.ToByte((Wint + 160).ToString("x"), 16);
                byte Lbyte = System.Convert.ToByte((Qint + 160).ToString("x"), 16);
                //将两个字节变量存储在字节数组中
                byte[] Nbytes = new byte[] { Fbyte, Lbyte };
                Chinese += Ecode.GetString(Nbytes);
                Rnd = null;
            }
            Ecode = null;
            return Chinese;
        }
        #endregion





        #region ASCII值转字符
        public static string GetStringFromAscii(string ascii)
        {
            string str = ascii;
            string str1 = string.Empty;
            for (int i = 0; i < str.Length; i++)
            {
                int j = int.Parse(str.Substring(i, 2));
                if (j < 64)
                {
                    j = int.Parse(str.Substring(i, 3));
                    i += 2;
                }
                else
                {
                    i += 1;
                }
                str1 += Encoding.ASCII.GetString(new byte[] { (byte)j });
            }

            return str1;

        }


        #endregion


        #region Base64字符串加密解密
        /// <summary>
        /// base64加密
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        public static string Base64En(string strs)
        {
            byte[] bytes = Encoding.Default.GetBytes(strs);
            string str = Convert.ToBase64String(bytes);
            return str;
        }
        /// <summary>
        /// base64 解密
        /// </summary>
        /// <param name="strbase64"></param>
        /// <returns></returns>
        public static string Base64De(string strbase64)
        {
            byte[] bytes = Convert.FromBase64String(strbase64);
            string de = Encoding.Default.GetString(bytes);
            return de;
        }

        #endregion


        #region 基础判别函数
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object) { return !IsNull(Object, false); }
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object, bool IsRemoveSpace) { return !IsNull(Object, IsRemoveSpace); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object) { return IsNull(Object, false); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object, bool IsRemoveSpace)
        {
            if (Object == null) return true;
            string Objects = Object.ToString();
            if (Objects == "") return true;
            if (IsRemoveSpace)
            {
                if (Objects.Replace(" ", "") == "") return true;
                if (Objects.Replace("　", "") == "") return true;
            }
            return false;
        }
        /// <summary>
        /// 对象是否为bool值
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool IsBool(object Object) { return IsBool(Object, false); }
        /// <summary>
        /// 判断是否为bool值
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="Default">默认bool值</param>
        /// <returns>bool值</returns>
        public static bool IsBool(object Object, bool Default)
        {
            if (IsNull(Object)) return Default;
            try { return bool.Parse(Object.ToString()); }
            catch { return Default; }
        }
        /// <summary>
        /// 是否邮件地址
        /// </summary>
        /// <param name="Mail">等待验证的邮件地址</param>
        /// <returns>bool</returns>
        public static bool IsMail(string Mail) { return Regex.IsMatch(Mail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); }
        /// <summary>
        /// 是否URL地址
        /// </summary>
        /// <param name="HttpUrl">等待验证的Url地址</param>
        /// <returns>bool</returns>
        public static bool IsHttp(string HttpUrl) { return Regex.IsMatch(HttpUrl, @"^(http|https):\/\/[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]{2,}[A-Za-z0-9\.\/=\?%\-&_~`@[\]:+!;]*$"); }
        /// <summary>
        /// 取字符左函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Left(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            return Object.ToString().Substring(0, Math.Min(Object.ToString().Length, MaxLength));
        }
        /// <summary>
        /// 取字符中间函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="StarIndex">开始的位置索引</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Mid(string Object, int StarIndex, int MaxLength)
        {
            if (IsNull(Object)) return "";
            if (StarIndex >= Object.Length) return "";
            return Object.Substring(StarIndex, MaxLength);
        }
        /// <summary>
        /// 取字符右函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Right(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            int i = Object.ToString().Length;
            if (i < MaxLength) { MaxLength = i; i = 0; } else { i = i - MaxLength; }
            return Object.ToString().Substring(i, MaxLength);
        }

        #endregion
        /// <summary>
        /// 获取地址的域名
        /// </summary>
        /// <param name="url">http://www.weibaohui.com</param>
        /// <returns>www.weibaohui.com</returns>
        public static string GetUrlDomainName(string url)
        {
            string p = @"http://(?<domain>[^/]*)";
            Regex reg = new Regex(p, RegexOptions.IgnoreCase);
            Match m = reg.Match(url);
            return m.Groups["domain"].Value;
        }
        /// <summary>
        /// 获取完整的url地址
        /// </summary>
        /// <param name="fromurl">参考地址，以http://开头</param>
        /// <param name="refurl">要组合的地址</param>
        /// <returns></returns>
        public static string GetFullUrl(string fromurl, string refurl)
        {
            string tempwords = string.Empty;
            if (refurl.StartsWith("http://")) tempwords = refurl;
            else if (refurl.StartsWith("/")) tempwords = "http://" + basic.GetUrlDomainName(fromurl) + refurl;
            else tempwords = fromurl.Substring(0, fromurl.LastIndexOf("/") + 1) + refurl;
            return tempwords;
        }


        public static string Post2Http(string url, Encoding en, string poststr, CookieContainer cc)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = cc;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "post";
                request.Timeout = 2000000000;
                //request.KeepAlive = true;
                byte[] bytes = en.GetBytes(poststr);
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                return str;
            }
            catch
            {

                return "";
            }
        }
        public static string Post2Http(string url, Encoding en, string poststr)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream requestStream = null;
            StreamReader reader = null;
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = new CookieContainer();
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "post";
                //  request.Timeout = 600000;
                // request.KeepAlive = true;
                byte[] bytes = en.GetBytes(poststr);
                request.ContentLength = bytes.Length;
                requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                response = (HttpWebResponse)request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                response.Close();
                request.Abort();

                return str;
            }
            catch
            {

                return "";
            }
            finally
            {
                if (reader != null) { reader.Close(); }
                if (requestStream != null) { requestStream.Close(); }
                if (response != null) { response.Close(); }
                if (request != null) { request.Abort(); }
            }
        }
        public static string Get2Http(string url, Encoding en)
        {


            try
            {
                HttpWebRequest request = null;


                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = new CookieContainer();
                request.Timeout = 20000;
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                response.Close();
                request.Abort();
                return str;
            }
            catch (SystemException er)
            {
                 FTchina.Library.LogHelper.Debug(url + "::" + er.Message);
                return "";
            }

        }

        public static string PostHttphtml(string url, Encoding en, string poststr)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = cc;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "post";
            request.Timeout = 0xea60;
            request.KeepAlive = true;
            byte[] bytes = en.GetBytes(poststr);
            request.ContentLength = bytes.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), en);
            string str = reader.ReadToEnd();
            reader.Close();
            response.Close();
            request.Abort();
            return str;



        }
        public static string GetHttphtml(string url, Encoding en)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = cc;
                request.Method = "GET";
                request.Accept = "*/*";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                request.KeepAlive = true;
                request.Timeout = 0xea60;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                cc.Add(response.Cookies);
                string str = reader.ReadToEnd();
                reader.Close();
                response.Close();
                request.Abort();
                return str;
            }
            catch (Exception exception)
            {
                 FTchina.Library.LogHelper.Debug(exception.Message);
                return "";
            }
        }








        #region 日志封装
        public static void Log(object logs)
        {
             FTchina.Library.LogHelper.Debug(logs.ToString());
        }
        #endregion

        #region 数据类型检查
        /// <summary>
        /// 校验手机号码是否符合标准。
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static bool ValidateMobile(string mobile)
        {
            if (string.IsNullOrEmpty(mobile))
                return false;

            return Regex.IsMatch(mobile, @"^(13|14|15|16|18|19)\d{9}$");
        }
        #endregion

    }
}